package com.lagou;

import com.lagou.client.RpcConsumer;
import com.lagou.client.RpcConsumerManager;
import com.lagou.curator.GetNoteCurator;
import com.lagou.req.RequestInfo;
import com.lagou.service.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@SpringBootApplication
public class ClientBootStrap {

    private static  ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();

    private static UserService proxy;

    public static void main(String[] args) throws InterruptedException {
        SpringApplication.run(ClientBootStrap.class, args);
        GetNoteCurator getNoteCurator = new GetNoteCurator();
//        getNoteCurator.initData();
//        getNoteCurator.subscribeChildChanges();
        executorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
//                System.out.println("1111111111111111111111111111111111111111");
                String currentKey = "";
                try {
//                    RpcConsumerManager.removeAllServer();
                    List<String> addressList = getNoteCurator.getAddressList();
                    RpcConsumerManager.rpcConsumerMapChanged(addressList);
                    Map<String, RpcConsumer> rpcConsumerMap = RpcConsumerManager.getRpcConsumerMap();
                    if (rpcConsumerMap.size() > 0) {
                        for (String key : rpcConsumerMap.keySet()) {
                            currentKey = key;
                            long beforeTime = System.currentTimeMillis();
                            proxy = (UserService) rpcConsumerMap.get(key).createProxy(UserService.class);
                            proxy.sayHello(key + " are you ok?");
                            long afterTime = System.currentTimeMillis();
                            long diff = afterTime - beforeTime;
                            RequestInfo requestInfo = new RequestInfo(Long.valueOf(diff).intValue(), afterTime);
                            if (requestInfo.getElapsedTime() > 5000) {
                                RpcConsumerManager.removeServer(key);
                                System.out.println(key + ":响应超时，断开服务器连接");
                                continue;
                            }else{
                                RpcConsumerManager.addRequestInfo(key,requestInfo);
                            }
//                            System.out.println(key + ":已响应");

                        }
                    }
                    System.out.println("服务在线数量:" + RpcConsumerManager.getRequestInfoMap().size());
                } catch (Exception e) {
                    RpcConsumerManager.rpcConsumerMapChanged(null);
                    RpcConsumerManager.removeRequestInfo(currentKey);
//                    e.printStackTrace();
                }

            }
        }, 5, 5, TimeUnit.SECONDS);
//        SpringApplication.run(ClientBootStrap.class, args);
//        Thread.sleep(Integer.MAX_VALUE);
    }


}
